package org.spring.springboot.app.hard.hardregister.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.base.Constants;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.hard.hardregister.dao.HardRegisterDao;
import org.spring.springboot.app.hard.hardregister.domain.po.HardRegisterPO;
import org.spring.springboot.app.hard.hardregister.domain.vo.*;
import org.spring.springboot.app.hard.hardregister.service.IHardRegisterService;
import org.spring.springboot.app.onlinepay.service.IOnlinePayService;
import org.spring.springboot.app.sys.systemoption.service.ISystemOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class HardRegisterServiceImpl implements IHardRegisterService {

    @Autowired
    private HardRegisterDao dao;

    @Autowired
    private ISystemOptionService systemOptionService;

    @Override
    public List<HardRegisterVO> findAll(HardRegisterSearchVO vo) {
        vo.setTwoDimensionUrl(systemOptionService.getString(Constants.DEVICE_REGISTER_URL));
        return dao.findAll(vo,vo.getPageNum(), vo.getPageSize());
    }
    @Override
    public HardRegisterPO find(String id){
        return  dao.selectByPrimaryKey(id);
    }

    @Override
    public R save(HardRegisterAddVO vo){
        R result = new R();
        HardRegisterPO po = new HardRegisterPO();
        po.setDeviceHardSign(vo.getHardSign());
        po.setDevicePassword(vo.getHardPassword());
        if(StringUtils.isBlank(vo.getHardType())){
            po.setDeviceHardId(vo.getHardId());
            vo.setHardType("1");
        }
        po.setDeviceHardType(vo.getHardType());

        // 验证是否重复
        if(dao.checkCountHardRegister(po)>1){
            return result.setType(Type.ExistError);
        }
        po.preInsert();
        if(dao.insertSelective(po)<1){
            return result.setType(Type.OperError);
        }
        return  result;

    }
    @Override
    public R update(HardRegisterUpdateVO vo){
        R result = new R();
        if(StringUtils.isBlank(vo.getId())){
            return  result.setType(Type.ParamValidateFail);
        }

        HardRegisterPO po = new HardRegisterPO();
        po.setDevicePassword(vo.getHardPassword());
        po.setDeviceHardSign(vo.getHardSign());
        po.setDeviceHardId(vo.getHardId());
        po.setId(vo.getId());
        // 验证是否重复
        if(dao.checkCountHardRegister(po)>1){
            return result.setType(Type.ExistError);
        }
        po.preUpdate();
        if(dao.updateByPrimaryKeySelective(po)<1){
            return result.setType(Type.OperError);
        }
        return  result;
    }
    @Override
    public R delete(String id){
        R result = new R();
        if(StringUtils.isBlank(id)){
            return  result.setType(Type.ParamValidateFail);
        }
        HardRegisterPO po = new HardRegisterPO();
        po.setId(id);
        po.preDelete();
        if(dao.updateByPrimaryKeySelective(po)<1){
            return result.setType(Type.OperError);
        }
        return  result;
    }
    @Override
    public R opExcelHardRegistr(List<ExcelHardRegisterVO> list){
        R result = new R();
        int addNum = 0;
        int parmNum = 0;
        int reNum=0;
        for (ExcelHardRegisterVO vo : list) {
            if(StringUtils.isBlank(vo.getDeviceHardId())||StringUtils.isBlank(vo.getDeviceHardId())||StringUtils.isBlank(vo.getDevicePassword())){
                parmNum++;
                continue;
            }
            HardRegisterPO po = new HardRegisterPO();
            po.setDevicePassword(vo.getDevicePassword());
            po.setDeviceHardSign(vo.getDeviceHardSign());
            po.setDeviceHardId(vo.getDeviceHardId());
            // 验证是否重复
            if(dao.checkCountHardRegister(po)>1){
                reNum++;
                continue;
            }
            po.preInsert();
            if(dao.insertSelective(po)>0){
                addNum++;
            }
        }
        result.setData("总共处理了"+list.size()+"条数据 : 添加了成功了"+addNum+"个条数据！有"+parmNum+"条数据不全,有"+reNum+"条数据重复添加");
        return  result;
    }

    @Override
    public List<ExcelHardRegisterUrlVO> findExcelAllList(){
        List<ExcelHardRegisterUrlVO> list = dao.findExcelURlAllList(systemOptionService.getString(Constants.DEVICE_REGISTER_URL));
        return  list;
    }



}
